package com.resolution.dietaryassessment.view.tcm.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.rxbus.RxBus;
import com.example.rxbus.bean.MsgEvent;
import com.example.rxbus.bean.TCMEvent;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.utils.FragAndActivityCommunication;
import com.resolution.dietaryassessment.view.tcm.adapter.SubjectAdapter;
import com.resolution.dietaryassessment.view.tcm.data.DataUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static android.content.ContentValues.TAG;


/**
 * @author By Resolusion
 * @Title: 问题frag
 * @Date: 2020/2/22
 * @Time: 21:59
 * @email Res01ution@foxmail.com
 */
public class TcmSubjectFragment extends Fragment implements View.OnClickListener{

    private FragAndActivityCommunication callback;
    private int subIndex = 0;

    private RecyclerView recyclerView;
    private View view;
    private TextView mTcmTitle;
    private Button next;

    private SubjectAdapter subjectAdapter;

    private List<String> mTitleList = new ArrayList<>();
    private List<List<String>> mSubjectAdapterList = new ArrayList<>();
    private List<Float> mFractionList = new ArrayList<>();
    private final static int CHECK_RESULT_ID = 0x000009;

    private float mFraction = 50;


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        callback = (FragAndActivityCommunication)context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSubjectList();
        initTitleList();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //通过参数中的布局填充获取对应布局
        view =inflater.inflate(R.layout.fragment_peace,container,false);

        initView();
        return view;
    }

    private void initView() {
        next = view.findViewById(R.id.next_subject);
        recyclerView = view.findViewById(R.id.tcm_recycler);
        mTcmTitle = view.findViewById(R.id.tcm_title);

        next.setOnClickListener(this);
        initRecyclerView();
    }

    private void initTitleList(){
        Collections.addAll(mTitleList,
                getResources().getString(R.string.tcm_one),
                getResources().getString(R.string.tcm_two),
                getResources().getString(R.string.tcm_three),
                getResources().getString(R.string.tcm_four),
                getResources().getString(R.string.tcm_five),
                getResources().getString(R.string.tcm_six),
                getResources().getString(R.string.tcm_seven),
                getResources().getString(R.string.tcm_eight),
                getResources().getString(R.string.tcm_nine));
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        refreshAdapter();
    }

    private void initFraction(){
        mFraction = 50;
    }

    private void initSubjectList(){
        mSubjectAdapterList.add(DataUtil.getPeaceData());
        mSubjectAdapterList.add(DataUtil.getQiDeficiency());
        mSubjectAdapterList.add(DataUtil.getYangDeficiency());
        mSubjectAdapterList.add(DataUtil.getYinDeficiency());
        mSubjectAdapterList.add(DataUtil.getPhlegmDamp());
        mSubjectAdapterList.add(DataUtil.getDampHeat());
        mSubjectAdapterList.add(DataUtil.getBloodStasis());
        mSubjectAdapterList.add(DataUtil.getQiDepression());
        mSubjectAdapterList.add(DataUtil.getSpecialGift());
    }

    /**
     * 刷新adapter
     */
    private void refreshAdapter(){
        subjectAdapter = new SubjectAdapter(mSubjectAdapterList.get(subIndex));
        recyclerView.setAdapter(subjectAdapter);
        mTcmTitle.setText(mTitleList.get(subIndex));
        subjectAdapter.setScore(multiple -> mFraction = mFraction + multiple * 3);
        subjectAdapter.getScore();
    }

    @SuppressLint("ResourceType")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_subject:
                subIndex++;
                refreshAdapter();
                mFractionList.add(mFraction);
                subjectAdapter.clearList();
                initFraction();
                if (subIndex == mSubjectAdapterList.size() - 1) {
                    next.setText("查看测试结果");
                    next.setId(CHECK_RESULT_ID);
                }
                break;
            case CHECK_RESULT_ID:
                refreshAdapter();
                mFractionList.add(mFraction);
                RxBus.getInstance().post(new TCMEvent<Object>(mFractionList));
                callback.onNextFragment(true);
                break;
        }
    }
}
